package day01;
import domain.Book;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.ArrayList;

@WebServlet("/yan")
public class Text  extends HttpServlet {
    ArrayList<Book> list=new ArrayList<>();
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            try {
                Class.forName("com.mysql.jdbc.Driver");
                Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/book_sys", "root", "root");
                Statement stat = con.createStatement();
                String sql="select BID,title,author from book";
                ResultSet res = stat.executeQuery(sql);
                while(res.next()){
                    String name = (String)res.getObject("author");
                    String BID = (String)res.getObject("BID");
                    String book_name =(String) res.getObject("title");
                    Book book=new Book(BID,book_name,name);
                    list.add(book);
                }
               req.setAttribute("list",list);
                RequestDispatcher requestDispatcher = req.getRequestDispatcher("/ycy.jsp");
                requestDispatcher.forward(req,resp);
                con.close();
                stat.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
